package cn.clouds234.xmldemo;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.StringWriter;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class ObjToXmlClient {

    public static void main(String[] args) {
        NewInc newInc = selectAll();
        // 对取出的数据进行操作
        List<NewInc> newIncsOne = new ArrayList<>();
        newIncsOne.add(newInc);
        NewIncXml xml = new NewIncXml();

        xml.setNewIncs(newIncsOne);

        StringWriter writer = null;
        try {
            JAXBContext context = JAXBContext.newInstance(NewIncXml.class);
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            writer = new StringWriter();
            writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
            marshaller.marshal(xml, writer);
        } catch (JAXBException e) {
            e.printStackTrace();
        }

        System.out.println(writer.toString());
    }

    /*
    查询数据库productionissue表数据，设置到NewInc
     */
    public static NewInc selectAll() {
        NewInc newInc = new NewInc();
        // 将User的属性映射到Product的属性
        newInc.setUserId("1");
        newInc.setTitle("标题");
        newInc.setContent("测试obj转xml");
        newInc.setDate(LocalDate.now().toString());
        newInc.setWsUser("name");
        newInc.setWsPassword("pass123");
        newInc.setCreatorContact("user@mail");
        newInc.setSource("固定");
        newInc.setPriority("6");
        newInc.setUrgency("02");
        newInc.setReflectCanal("14");
        newInc.setIsEffectBusiness("是");
        newInc.setEffectSituation("file");
        newInc.setIsAccount("否");
        newInc.setIsRegulator("否");
        newInc.setIsBigCustomer("否");
        return newInc;
    }

}

